package top.hcode.hoj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.hcode.hoj.pojo.entity.course.Lesson;

@Mapper
@Repository
public interface LessonMapper extends BaseMapper<Lesson> {


    @Select("select max(sort_order) from lesson where section_id = ${sectionId}")
    Integer getNextSortOrder(@Param("sectionId") Long sectionId);

    @Update("update lesson set handout_count = (select count(*) from lesson_handout where lesson_id = #{lessonId}) where id = #{lessonId}")
    void refreshHandoutCount(@Param("lessonId") Long lessonId);

    @Update("update lesson set " +
            "lesson_work_count = (select count(*) from lesson_work_question where lesson_id = #{lessonId} and work_type = 'lesson')," +
            "home_work_count   = (select count(*) from lesson_work_question where lesson_id = #{lessonId} and work_type = 'home') " +
            "where id = #{lessonId}")
    void refreshWorkCount(@Param("lessonId") Long lessonId);
}
